<?php
//----websocket class created by warren
//namespace Common\Library;

// ini_set('display_errors', 1);
// error_reporting(E_ALL);

/**
 * Very basic websocket client.
 * Supporting draft hybi-10. 
 * 
 * @author Simon Samtleben <web@lemmingzshadow.net>
 * @version 2011-10-18
 */

class WebSocketClient
{
	private $_host;
	private $_port;
	private $_path;
	private $_origin;
	private $_Socket = null;
	private $_connected = false;
	
	public function __construct() { }
	
	public function __destruct()
	{
		$this->disconnect();
	}

	public function sendData($data, $type = 'text', $masked = true)
	{
		if($this->_connected === false)
		{
			trigger_error("Not connected", E_USER_WARNING);
			return false;
		}
		
		if( !is_string($data)) {
			trigger_error("Not a string data was given.", E_USER_WARNING);
			return false;
		}
		if (strlen($data) == 0)
		{
			return false;
		}
		$res = @fwrite($this->_Socket, $this->_hybi10Encode($data, $type, $masked));
		
		if($res === 0 || $res === false)
		{
			return false;
		}
		$buffer = ' ';
		while($buffer !== '')
		{
			$buffer = fread($this->_Socket, 512);
		}
		return true;
	}

	public function connect($host, $port, $path, $origin = false)
	{
		$this->_host = $host;
		$this->_port = $port;
		$this->_path = $path;
		$this->_origin = $origin;
		
		$key = base64_encode($this->_generateRandomString(16, false, true));
		$header = "GET " . $path . " HTTP/1.1\r\n";
		$header.= "Host: ".$host.":".$port. "\r\n";
		$header.= "Upgrade: websocket\r\n";
		$header.= "Connection: Upgrade\r\n";
		//$header.= "Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits\r\n";
		$header.= "Sec-WebSocket-Key: " . $key . "\r\n";

		if($origin !== false)
		{
			$header.= "Sec-WebSocket-Origin: " . $origin . "\r\n";
		}
		$header.= "Sec-WebSocket-Version: 13\r\n\r\n";
		
		$this->_Socket = fsockopen($host, $port, $errno, $errstr, 2);
		socket_set_timeout($this->_Socket, 2, 10000);
		//socket_write($this->_Socket, $header);
		$res = @fwrite($this->_Socket, $header);
		if( $res === false ){
			echo "fwrite false \n";
		}
		
		$response = @fread($this->_Socket, 1500);
		//$response = socket_read($this->_Socket);
		preg_match('#Sec-WebSocket-Accept:\s(.*)$#mU', $response, $matches);
		if ($matches) {
			$keyAccept = trim($matches[1]);
			$expectedResonse = base64_encode(pack('H*', sha1($key . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11')));
			$this->_connected = ($keyAccept === $expectedResonse) ? true : false;
		}
		return $this->_connected;
	}
	
	public function checkConnection()
	{
		$this->_connected = false;
		
		// send ping:
		$data = 'ping?';
		@fwrite($this->_Socket, $this->_hybi10Encode($data, 'ping', true));
		$response = @fread($this->_Socket, 300);
		if(empty($response))
		{
			return false;
		}
		$response = $this->_hybi10Decode($response);
		if(!is_array($response))
		{
			return false;
		}
		if(!isset($response['type']) || $response['type'] !== 'pong')
		{
			return false;
		}
		$this->_connected = true;
		return true;
	}


	public function disconnect()
	{
		$this->_connected = false;
		is_resource($this->_Socket) and fclose($this->_Socket);
	}
	
	public function reconnect()
	{
		sleep(10);
		$this->_connected = false;
		fclose($this->_Socket);
		$this->connect($this->_host, $this->_port, $this->_path, $this->_origin);
	}

	private function _generateRandomString($length = 10, $addSpaces = true, $addNumbers = true)
	{
		$characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"ยง$%&/()=[]{}';
		$useChars = array();
		// select some random chars:
		for($i = 0; $i < $length; $i++)
		{
			$useChars[] = $characters[mt_rand(0, strlen($characters)-1)];
		}
		// add spaces and numbers:
		if($addSpaces === true)
		{
			array_push($useChars, ' ', ' ', ' ', ' ', ' ', ' ');
		}
		if($addNumbers === true)
		{
			array_push($useChars, rand(0,9), rand(0,9), rand(0,9));
		}
		shuffle($useChars);
		$randomString = trim(implode('', $useChars));
		$randomString = substr($randomString, 0, $length);
		return $randomString;
	}
	
	private function _hybi10Encode($payload, $type = 'text', $masked = true)
	{
		$frameHead = array();
		$frame = '';
		$payloadLength = strlen($payload);
		
		switch($type)
		{
			case 'text':
				// first byte indicates FIN, Text-Frame (10000001):
				$frameHead[0] = 129;
			break;
		
			case 'close':
				// first byte indicates FIN, Close Frame(10001000):
				$frameHead[0] = 136;
			break;
		
			case 'ping':
				// first byte indicates FIN, Ping frame (10001001):
				$frameHead[0] = 137;
			break;
		
			case 'pong':
				// first byte indicates FIN, Pong frame (10001010):
				$frameHead[0] = 138;
			break;
		}
		
		// set mask and payload length (using 1, 3 or 9 bytes)
		if($payloadLength > 65535)
		{
			$payloadLengthBin = str_split(sprintf('%064b', $payloadLength), 8);
			$frameHead[1] = ($masked === true) ? 255 : 127;
			for($i = 0; $i < 8; $i++)
			{
				$frameHead[$i+2] = bindec($payloadLengthBin[$i]);
			}
			// most significant bit MUST be 0 (close connection if frame too big)
			if($frameHead[2] > 127)
			{
				$this->close(1004);
				return false;
			}
		}
		elseif($payloadLength > 125)
		{
			$payloadLengthBin = str_split(sprintf('%016b', $payloadLength), 8);
			$frameHead[1] = ($masked === true) ? 254 : 126;
			$frameHead[2] = bindec($payloadLengthBin[0]);
			$frameHead[3] = bindec($payloadLengthBin[1]);
		}
		else
		{
			$frameHead[1] = ($masked === true) ? $payloadLength + 128 : $payloadLength;
		}

		// convert frame-head to string:
		foreach(array_keys($frameHead) as $i)
		{
			$frameHead[$i] = chr($frameHead[$i]);
		}
		if($masked === true)
		{
			// generate a random mask:
			$mask = array();
			for($i = 0; $i < 4; $i++)
			{
				$mask[$i] = chr(rand(0, 255));
			}
			
			$frameHead = array_merge($frameHead, $mask);
		}
		$frame = implode('', $frameHead);

		// append payload to frame:
		$framePayload = array();
		for($i = 0; $i < $payloadLength; $i++)
		{
			$frame .= ($masked === true) ? $payload[$i] ^ $mask[$i % 4] : $payload[$i];
		}

		return $frame;
	}
	
	private function _hybi10Decode($data)
	{
		$payloadLength = '';
		$mask = '';
		$unmaskedPayload = '';
		$decodedData = array();
		
		// estimate frame type:
		$firstByteBinary = sprintf('%08b', ord($data[0]));
		$secondByteBinary = sprintf('%08b', ord($data[1]));
		$opcode = bindec(substr($firstByteBinary, 4, 4));
		$isMasked = ($secondByteBinary[0] == '1') ? true : false;
		$payloadLength = ord($data[1]) & 127;
		
		switch($opcode)
		{
			// text frame:
			case 1:
				$decodedData['type'] = 'text';
			break;
		
			case 2:
				$decodedData['type'] = 'binary';
			break;
			
			// connection close frame:
			case 8:
				$decodedData['type'] = 'close';
			break;
			
			// ping frame:
			case 9:
				$decodedData['type'] = 'ping';
			break;
			
			// pong frame:
			case 10:
				$decodedData['type'] = 'pong';
			break;
			
			default:
				return false;
			break;
		}
		
		if($payloadLength === 126)
		{
		   $mask = substr($data, 4, 4);
		   $payloadOffset = 8;
		   $dataLength = bindec(sprintf('%08b', ord($data[2])) . sprintf('%08b', ord($data[3]))) + $payloadOffset;
		}
		elseif($payloadLength === 127)
		{
			$mask = substr($data, 10, 4);
			$payloadOffset = 14;
			$tmp = '';
			for($i = 0; $i < 8; $i++)
			{
				$tmp .= sprintf('%08b', ord($data[$i+2]));
			}
			$dataLength = bindec($tmp) + $payloadOffset;
			unset($tmp);
		}
		else
		{
			$mask = substr($data, 2, 4);
			$payloadOffset = 6;
			$dataLength = $payloadLength + $payloadOffset;
		}
		
		if($isMasked === true)
		{
			for($i = $payloadOffset; $i < $dataLength; $i++)
			{
				$j = $i - $payloadOffset;
				if(isset($data[$i]))
				{
					$unmaskedPayload .= $data[$i] ^ $mask[$j % 4];
				}
			}
			$decodedData['payload'] = $unmaskedPayload;
		}
		else
		{
			$payloadOffset = $payloadOffset - 4;
			$decodedData['payload'] = substr($data, $payloadOffset);
		}
		
		return $decodedData;
	}
}

//----end websocket classcreated by warren

require_once ('/opt/ethos/lib/minerprocess.php');

function get_http_response_code($url)
{
	$headers = get_headers($url);
	return substr($headers[0], 9, 3);
}

function sample_kbps(){

	$rx_now = `tail -1 /var/log/rx.log`;
	$rx_before = `tail -2 /var/log/rx.log | head -1`;

        $tx_now = `tail -1 /var/log/tx.log`;
        $tx_before = `tail -2 /var/log/tx.log | head -1`;

	$rx_kbps = sprintf("%.2f",($rx_now-$rx_before)/600/1024);
	$tx_kbps = sprintf("%.2f",($tx_now-$tx_before)/600/1024);

	return array($rx_kbps,$tx_kbps);
}

function putconf($interactive = "0")
{
	`sudo /opt/ethos/sbin/ethos-motd-generator`;

	if ($interactive != "1") {
		`/sbin/ifconfig | grep bytes | sed 's/bytes/*/g' | sed 's/://g' | sed 's/ (/*/g' | cut -d"*" -f2,4 | head -1 | cut -d"*" -f1 >> /var/log/rx.log`;
		`/sbin/ifconfig | grep bytes | sed 's/bytes/*/g' | sed 's/://g' | sed 's/ (/*/g' | cut -d"*" -f2,4 | head -1 | cut -d"*" -f2 >> /var/log/tx.log`;
		sleep(mt_rand(0, 5)); //do not saturate webserver children with requests if run automatically
	}

	$remote = str_replace("\r\n", "\n", trim(`cat /home/ethos/remote.conf | grep -v '^#' | grep '\.' | head -1`));
	$send_remote = substr($remote, 0, 150);
	file_put_contents("/var/run/ethos/send_remote.file", $send_remote);
	if (strlen($remote) == 0 || !eregi("http://|https://", $remote) || substr($remote, 0, 1) == "#") {
		$message = "REMOTE CONFIG DOES NOT EXIST OR IS FORMATTED INCORRECTLY. USING LOCAL CONFIG.";
		echo $message . "\n";
		if (!$remote) {
			file_put_contents("/var/run/ethos/config_mode.file", "singlerig");
		}
		else {
			file_put_contents("/var/run/ethos/config_mode.file", "badformat");
		}

		return;
	}
	else {
		ini_set('default_socket_timeout', 3);
		$header = get_http_response_code($remote);
		if ($interactive == "1") {
			echo " ...";
		}
	}
        $header_array = array(200, 301, 302);
	if (!in_array($header, $header_array)) {
		$message = "REMOTELY DEFINED CONFIG SERVER IS UNREACHABLE. USING LOCAL CONFIG.";
		echo $message . "\n";
		file_put_contents("/var/run/ethos/config_mode.file", "unreachable");
		return;
	}
	else {
		$remote = trim($remote);
		$remote_contents = str_replace("\r\n", "\n", trim(file_get_contents($remote, FILE_IGNORE_NEW_LINES)));
		if ($interactive == "1") {
			echo "...";
		}
	}

	if (preg_match("/<[^<]+>/", $remote_contents, $m) != 0) {
		$message = "REMOTE CONFIG CONTAINS HTML OR XML TAGS. USING LOCAL CONFIG.";
		echo $message . "\n";
		file_put_contents("/var/run/ethos/config_mode.file", "invalid");
		return;
	}
	else {
		if ($interactive == "1") {
			echo "...";
		}
	}

	if (strlen($remote_contents) < 15) {
		$message = "REMOTE CONFIG APPEARS TO BE TOO SHORT. USING LOCAL CONFIG.";
		echo $message . "\n";
		file_put_contents("/var/run/ethos/config_mode.file", "tooshort");
		return;
	}
	else {
		if ($interactive == "1") {
			echo "...";
		}
	}

	if (md5(trim($remote_contents)) != md5(trim(file_get_contents("/home/ethos/local.conf")))) {
		$message = "IMPORTED REMOTE CONFIG INTO LOCAL CONFIG.";
		echo $message . "\n";
		file_put_contents("/home/ethos/local.conf", $remote_contents . "\n");
	}
	else {
		$message = "REMOTE CONFIG CONTENTS MATCHES LOCAL CONFIG.";
		echo $message . "\n";
		
		if ($interactive == "1") {
			echo "...";
		}
	}
	
	$local = str_replace("\r\n", "\n", trim(@file_get_contents("/home/ethos/local.conf")));
	if (md5($local) != md5(trim(@file_get_contents("/home/ethos/local.conf")))) {
		file_put_contents("/home/ethos/local.conf", $local);
	}
	
}

function check_proxy()
{
	$miner = trim(`/opt/ethos/sbin/ethos-readconf miner`);
	$stratumtype = trim(`/opt/ethos/sbin/ethos-readconf stratumenabled`);

		file_put_contents("/var/run/ethos/proxy_error.file","working");
		$proxy_error = 0;

	if ($miner == "ethminer" && $stratumtype == "enabled") {
		$requested_restart = trim(`tail -100 /var/run/ethos/proxy.output | grep 'Please restart proxy' | wc -l`);
		if ($requested_restart > 0) {
			file_put_contents("/var/run/ethos/proxy_error.file","restart");
			$proxy_error = 2;
		}

		$primary_pool_offline = trim(`tail -100 /var/run/ethos/proxy.output | grep 'must be online' | wc -l`);
		if ($primary_pool_offline > 0) {
			file_put_contents("/var/run/ethos/proxy_error.file","primary_down");
			$proxy_error = 3;
		}

		$proxy_getting_job = trim(`tail -5 /var/run/proxy.output | grep 'NEW_JOB MAIN_POOL' | wc -l`);
		$rpc_problems = trim(`tail -240 /var/run/miner.0.output | grep 'JSON-RPC problem' | wc -l`);
		if ($rpc_problems >= 30 && $proxy_getting_job > 2) {
			file_put_contents("/var/run/ethos/proxy_error.file","failure");
			$proxy_error = 4;
		}

		$rejected_shares = trim(`tail -100 /var/run/ethos/proxy.output | grep -c "REJECTED"`);
		if ($rejected_shares > 2) {
			file_put_contents("/var/run/ethos/proxy_error.file","rejected");
			$proxy_error = 5;
		}

		if ($proxy_error > 0) {
			`echo -n "" > /var/run/ethos/proxy.output`;
			`killall -9 python`;
			`su - ethos -c '/opt/eth-proxy/eth-proxy.py >> /var/run/ethos/proxy.output 2>&1 &'`;
		}
	}
}

function stratum_phoenix()
{
	`ps uax| grep "python /opt/eth-proxy/eth-proxy.py" | grep -v grep | awk '{print $2}' | xargs kill -9 2> /dev/null`;
	`su - ethos -c '/opt/eth-proxy/eth-proxy.py >> /var/run/ethos/proxy.output 2>&1 &'`;
}

function get_stats()
{
	$gpus = trim(file_get_contents("/var/run/ethos/gpucount.file"));
	$driver = trim(`/opt/ethos/sbin/ethos-readconf driver`);
	$miner = trim(`/opt/ethos/sbin/ethos-readconf miner`);
	list($rx_kbps,$tx_kbps) = sample_kbps();

	// miner check info
	$send['defunct'] = intval(trim(file_get_contents("/var/run/ethos/defunct.file")));
	
	$send['publicip'] = trim(file_get_contents("/var/run/ethos/panel.file"));
	
	$send['off'] = trim(`/opt/ethos/sbin/ethos-readconf off`);
	$send['allowed'] = intval(trim(file_get_contents("/opt/ethos/etc/allow.file")));
	$send['overheat'] = intval(trim(file_get_contents("/var/run/ethos/overheat.file")));
	$send['pool_info'] = trim(`cat /home/ethos/local.conf | grep -v '^#' | egrep -i 'pool|wallet|proxy'`);
	$send['pool'] = trim(`/opt/ethos/sbin/ethos-readconf proxypool1`);
	$send['miner_version'] = trim(`cat /var/run/ethos/miner.versions | grep '$miner ' | cut -d" " -f2 | head -1`);

	// system related info

	$send['rx_kbps'] = $rx_kbps;
	$send['tx_kbps'] = $tx_kbps;
	$send['kernel'] = trim(`/opt/ethos/sbin/ethos-readdata kernelversion`);
	$send['boot_mode'] = trim(`/opt/ethos/sbin/ethos-readdata bootmode`);
	$send['uptime'] = trim(`cat /proc/uptime | cut -d"." -f1`);
	$send['mac'] = trim(`/opt/ethos/sbin/ethos-readdata macid`);
	$send['hostname'] = trim(`/opt/ethos/sbin/ethos-readdata hostname`);
	$send['rack_loc'] = trim(`/opt/ethos/sbin/ethos-readconf loc`);
	$send['ip'] = trim(`/opt/ethos/sbin/ethos-readdata ip`);
	$send['manu'] = trim(file_get_contents("/var/run/ethos/manu.file"));
	$send['mobo'] = trim(file_get_contents("/var/run/ethos/motherboard.file"));
	$send['lan_chip'] = trim(file_get_contents("/var/run/ethos/lan.file"));
	$load = sys_getloadavg();
	$send['load'] = trim($load[2]);
	$send['ram'] = trim(`/opt/ethos/sbin/ethos-readdata ramsize`);
	$send['cpu_temp'] = trim(file_get_contents("/var/run/ethos/cputemp.file"));
	$send['cpu_name'] = trim(@file_get_contents("/var/run/ethos/cpuinfo.file"));
	$send['rofs'] = time() - trim(file_get_contents("/opt/ethos/etc/check-ro.file"));
	$send['drive_name'] = trim(`/opt/ethos/sbin/ethos-readdata driveinfo`);
	$send['freespace'] = round(trim(`/opt/ethos/sbin/ethos-readdata freespace`) / 1024 / 1024, 1);
	$send['temp'] = trim(`/opt/ethos/sbin/ethos-readdata temps`);
	$send['version'] = trim(file_get_contents("/opt/ethos/etc/version"));
	$send['miner_secs'] = 0 + trim(`/opt/ethos/sbin/ethos-readdata mineruptime`);
	$send['adl_error'] = trim(file_get_contents("/var/run/ethos/adl_error.file"));
	$send['proxy_problem'] = trim(file_get_contents("/var/run/ethos/proxy_error.file"));
	$send['updating'] = trim(file_get_contents("/var/run/ethos/updating.file"));
	$send['connected_displays'] = trim(`/opt/ethos/sbin/ethos-readdata connecteddisplays`);
	$send['resolution'] = trim(`/opt/ethos/sbin/ethos-readdata resolution`);
	$send['gethelp'] = trim(`tail -1 /var/log/gethelp.log`);
	$send['config_status'] = trim(@file_get_contents("/var/run/ethos/config_mode.file"));
	$send['send_remote'] = trim(@file_get_contents("/var/run/ethos/send_remote.file"));
	$send['autorebooted'] = trim(@file_get_contents("/opt/ethos/etc/autorebooted.file"));
	$send['status'] = trim(@file_get_contents("/var/run/ethos/status.file"));

	// gpu related info

	$send['driver'] = trim(`/opt/ethos/sbin/ethos-readconf driver`);
	$send['selected_gpus'] = trim(`/opt/ethos/sbin/ethos-readconf selectedgpus`);
	$send['crashed_gpus'] = trim(file_get_contents("/var/run/ethos/crashed_gpus.file"));

	$send['gpus'] = $gpus;
	$send['fanrpm'] = trim(`/opt/ethos/sbin/ethos-readdata fanrpm | xargs | tr -s ' '`);
	$send['fanpercent'] = trim(`/opt/ethos/sbin/ethos-readdata fan | xargs | tr -s ' '`);
	$send['hash'] = trim(`tail -10 /var/run/ethos/miner_hashes.file | sort -V | tail -1 | tr ' ' '\n' | awk '{sum += \$1} END {print sum}'`);
	$send['miner'] = $miner;
	$send['miner_hashes'] = trim(`tail -10 /var/run/ethos/miner_hashes.file | sort -V | tail -1`);
	if($miner == "claymore"){
		$send['dualminer_status'] = trim (`/opt/ethos/sbin/ethos-readconf dualminer`);
		$send['dualminer_coin'] = trim (`/opt/ethos/sbin/ethos-readconf dualminer-coin`);
		$send['dualminer_hashes'] = trim(`tail -10 /var/run/ethos/dualminer_hashes.file | sort -V | tail -1`);
	}
	if(file_exists("/home/ethos/claymore.flags") || file_exists("/home/ethos/claymore-zcash.flags")) {
		$send['stub_flags_present'] = "1";
	}
	if(preg_match("/sgminer/",$miner)){
		$send['hwerrors'] = trim(file_get_contents("/var/run/ethos/hwerror.file"));
	}
	$send['models'] = trim(file_get_contents("/var/run/ethos/gpulist.file"));
	$send['bioses'] = trim(trim(`/opt/ethos/sbin/ethos-readdata bios | xargs | tr -s ' '`));
	$send['default_core'] = trim(file_get_contents("/var/run/ethos/defaultcore.file"));
	$send['default_mem'] = trim(file_get_contents("/var/run/ethos/defaultmem.file"));
	$send['vramsize'] = trim(file_get_contents("/var/run/ethos/vrams.file"));
	$send['core'] = trim(`/opt/ethos/sbin/ethos-readdata core | xargs | tr -s ' '`);
	$send['mem'] = trim(`/opt/ethos/sbin/ethos-readdata mem | xargs | tr -s ' '`);
        $send['memstates'] = trim(`/opt/ethos/sbin/ethos-readdata memstate | xargs | tr -s ' '`);
	$send['meminfo'] = trim(file_get_contents("/var/run/ethos/meminfo.file"));
	$send['voltage'] = trim(`/opt/ethos/sbin/ethos-readdata voltage | xargs | tr -s ' '`);
	
	if($driver == "nvidia"){
		$send['nvidia_error'] = trim(file_get_contents("/var/run/ethos/nvidia_error.file"));
		$send['watts'] = trim(`/opt/ethos/sbin/ethos-readdata watts | xargs | tr -s ' '`);
		$send['watt_min'] = trim(file_get_contents("/var/run/ethos/watt_min.file"));
		$send['watt_max'] = trim(file_get_contents("/var/run/ethos/watt_max.file"));
	}
	$send['overheatedgpu'] = trim(file_get_contents("/var/run/ethos/overheatedgpu.file"));
	$send['throttled'] = trim(file_get_contents("/var/run/ethos/throttled.file"));
	$send['powertune'] = trim(`/opt/ethos/sbin/ethos-readdata powertune | xargs | tr -s ' '`);

	return $send;
}

function send_data()
{
	//this is added by warren
	// 使用 WebSocket 通知客户端
//	$client = new WebSocketClient();
//	$client->connect("60.2.177.174", 8081, '/');
	//end write by warren
	$send = get_stats();

	$private_hash = trim(file_get_contents("/var/run/ethos/private_hash.file"));
	$public_hash = trim(file_get_contents("/var/run/ethos/panel.file"));
	$hook = "http://ethosdistro.com/get.php";
	$url = "http://" . $public_hash . ".ethosdistro.com/";
	$json = json_encode($send);
	$log = "";
	foreach($send as $key => $data) {
		$log.= "$key:$data\n";
	}

	$url_style = urlencode($json);
	echo "$json";
//	echo "$url_style";
//	echo "$private_hash";
//	$client->sendData($json);
//	$client->sendData($url_style);

	$hostname = $send['hostname'];
	//$message = file_get_contents("$hook?hostname=$hostname&url_style=$url_style&hash=$private_hash");
	$message = "Daspack system created by dastrong";
	
	if (md5(trim($message)) != md5(trim(file_get_contents("/opt/ethos/etc/message")))) {
		file_put_contents("/opt/ethos/etc/message", trim($message));
	}

//	$client->disconnect();
	
	return $log;
}

function prevent_overheat()
{
	$max_temp = trim(`/opt/ethos/sbin/ethos-readconf maxtemp`);
	if (!$max_temp) {
		$max_temp = "85";
	}
	$throttle_temp = ($max_temp - 5);
	$miner = trim(`/opt/ethos/sbin/ethos-readconf miner`);
	if(preg_match("/sgminer/",$miner)){
		$max_temp = ($max_temp + 3);
	}
	$temps = trim(`/opt/ethos/sbin/ethos-readdata temps`);
	$temp_array = explode(" ", $temps);
	$c = 0;
	$bad_values = "108 115 128 135";
	$bad_array = explode(" ", $bad_values);
	foreach($temp_array as $temp) {
		$throttled[$c] = trim(file_get_contents("/var/run/ethos/throttled.gpu" . $c));
		if ($temp > $throttle_temp && $temp < 500 && !in_array($temp, $bad_array) && !$throttled[$c]) {
			`/opt/ethos/sbin/ethos-throttle $c`;
			file_put_contents("/var/run/ethos/throttled.file", "1");
			file_put_contents("/var/run/ethos/throttled.gpu" . $c, "1");
		}

		if ($temp > $max_temp && $temp < 500 && !in_array($temp, $bad_array)) {
			$pid = trim(`/opt/ethos/sbin/ethos-readconf pid $c`);
			`kill -9 $pid 2> /dev/null`;
			file_put_contents("/var/run/ethos/overheat.file", "1");
			file_put_contents("/var/run/ethos/overheatedgpu.file", "$c");
			break;
		}

		$c++;
	}
}

?>

